<?php
/**
 * Created by PhpStorm.
 * User: Simpson
 * Date: 2016/10/9
 * Time: 17:42
 */

namespace App\Service;


use App\Repository\LotteryRepository;
class LotteryService
{
    public $lottery;

    /**
     * 注入数据库操作对象
     * 
     * LotteryService constructor.
     * @param LotteryRepository $lottery
     */
    public function __construct(LotteryRepository $lottery)
    {
        $this->lottery = $lottery;
    }

    /**
     * 抽取奖品
     *
     * @return mixed
     */
    public function prize()
    {
        $openId = \Auth::guard('wechat')->user()->open_id;
        if($this->lottery->checkLottery($openId))
            return false;
        
        $random = random_int(0, 100);
        $prize = $this->lottery->getPrize($random);
        $result = $this->lottery->addLottery($openId, $prize->id);

        return $result ? $prize : false;
    }

    /**
     * 奖品列表
     * 
     * @return mixed
     */
    public function prizeList()
    {
        $prize_list = $this->lottery->prizeList();

        return $prize_list;
    }

    public function lotteryList()
    {
        $lottery_list = $this->lottery->lotteryList();

        return $lottery_list;
    }

}